/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = activemq_core.c                                    */
/*  NOTE      =                                                    */
/*  DATE      = 2015/04/28 by geyh                                 */
/*******************************************************************/
#include <stdio.h>
#include <sys/time.h>

#include "frmem.h"
#include "frcomm.h"
#include "frlog.h"
#include "frhash2.h"
#include "frreport_comm.h"
#include "frct_comm.h"
#include "activemq_core.h"
#include "activemq_parse.h"
#include "frservice_comm.h"
#include "config.h"

extern void *g_p_apm_context;

/*
*   activemq session 数据上报
*/
FR_STATIC int __activemq_session_report(Frreport_body_data *p_st_body_data, L7session_activeMQ_private *p_st_private)
{
    char arr_tmp[1024];
    if(p_st_body_data == NULL || p_st_private == NULL)
        return FR_FAIL;

    if(p_st_private->st_login_timestamp.tv_sec)
    {
        if(p_st_private->p_user_name)
        {
            if(frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_SESSION_USERNAME, p_st_private->p_user_name) == FR_FAIL)
                return FR_FAIL;
        }
        else
        {
            if(frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_SESSION_USERNAME, "\0") == FR_FAIL)
                return FR_FAIL;
        }
        if(frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_SESSION_ID, p_st_private->arr_session_id) == FR_FAIL)
            return FR_FAIL;

        if(frreport_body_l7_add_timeval(p_st_body_data, APM_REPORT_KEY_SESSION_LOGIN_TIMESTAMP, &p_st_private->st_login_timestamp) == FR_FAIL)
            return FR_FAIL;
    }

    if(p_st_private->p_server)
    {
        if(frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_SESSION_SERVER, p_st_private->p_server) == FR_FAIL)
            return FR_FAIL;
    }

    if(p_st_private->p_title)
    {
        if(p_st_private->i_use_request)
        {
            if(p_st_private->is_messages)
                snprintf(arr_tmp, sizeof(arr_tmp), "PUT %s", p_st_private->p_title);
            else
                snprintf(arr_tmp, sizeof(arr_tmp), "SUB %s", p_st_private->p_title);
        }
        else
        {
            snprintf(arr_tmp, sizeof(arr_tmp), "NOTIFY %s", p_st_private->p_title);
        }

        //title
        if(frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_TRANS_TITLE, arr_tmp) == FR_FAIL)
            return FR_FAIL;

        //session
        if(p_st_private->is_topic)
        {
            if(frreport_body_l7_add_str(p_st_body_data, "session.mq.topic", p_st_private->p_title) == FR_FAIL)
                return FR_FAIL;
        }
        else
        {
            if(frreport_body_l7_add_str(p_st_body_data, "session.mq.queue", p_st_private->p_title) == FR_FAIL)
                return FR_FAIL;
        }

    }
    else
    {
        if(p_st_private->i_use_request)
        {
            if(p_st_private->is_messages)
            {
                if(frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_TRANS_TITLE, "PUT") == FR_FAIL)
                    return FR_FAIL;
            }
            else
            {
                if(frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_TRANS_TITLE, "SUB") == FR_FAIL)
                    return FR_FAIL;
            }
        }
        else
        {
            if(frreport_body_l7_add_str(p_st_body_data, APM_REPORT_KEY_TRANS_TITLE, "NOTIFY") == FR_FAIL)
                return FR_FAIL;
        }
    }

    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  activeMQ私有信息创建
*  ARGS     :  void
*           :
*  RTN      :  NULL                     失败
*           :  p_st_private             成功
*  NOTE     :
*******************************************************************************/
FR_STATIC L7session_activeMQ_private* __activeMQ_private_create(void)
{
    L7session_activeMQ_private *p_st_private = NULL;

    p_st_private = (L7session_activeMQ_private*)frmalloc(sizeof(L7session_activeMQ_private));
    if(p_st_private == NULL)
    {
        FRLOG_ERROR_DETAIL("MEMORY ERR");
        return NULL;
    }

    memset(p_st_private, 0x00, sizeof(L7session_activeMQ_private));

    p_st_private->i_tightEncodingEnabled = 1;
    p_st_private->i_cacheEnabled = 1;
    p_st_private->i_flag_try = 1;
    p_st_private->i_flag_detected = 1;
    p_st_private->p_st_data = bytebuff_creat();
    if(p_st_private->p_st_data == NULL)
    {
        FRLOG_ERROR_DETAIL("MEMORY ERR");
        frfree(p_st_private);
        return NULL;
    }

    return p_st_private;

}

/*******************************************************************************
*  FUNC     :  私有信息销毁
*  ARGS     :  p_private_session(activeMQ的私有session数据)
*  RTN      :  FR_SUCC
*  NOTE     :
*******************************************************************************/
FR_STATIC int __activeMQ_session_reset(void *p_private_session)
{
    L7session_activeMQ_private *p_st_private = (L7session_activeMQ_private*)p_private_session;
    if(p_st_private)
    {
        if(p_st_private->p_st_body_data_req)
            frreport_body_free(&p_st_private->p_st_body_data_req);

        if(p_st_private->p_st_body_data_resp)
            frreport_body_free(&p_st_private->p_st_body_data_resp);

        if(p_st_private->p_lst_comm_tmp)
            frreport_body_l7list_free(&p_st_private->p_lst_comm_tmp);

        if(p_st_private->p_lst_req_tmp)
            frreport_body_l7list_free(&p_st_private->p_lst_req_tmp);

        if(p_st_private->p_lst_mq_tmp)
            frreport_body_l7list_free(&p_st_private->p_lst_mq_tmp);

        if(p_st_private->p_st_data)
            bytebuff_free(&p_st_private->p_st_data);

        p_st_private->p_st_data = bytebuff_creat();
        if(p_st_private->p_st_data == NULL)
            return FR_FAIL;

        memset(&p_st_private->st_l4_trans, '\0', sizeof(L4_transaction));
        p_st_private->i_flag_data_type = 0;
        p_st_private->i_data_len = 0;
        p_st_private->is_messages = 0;
        p_st_private->i_flag_report = 0;

    }
    return FR_SUCC;
}



/*******************************************************************************
*  FUNC     :  activeMQ req 的packet解析
*  ARGS     :  p_st_conn                (入力链接信息)
            :  p_st_packet             (入力packet信息)
            :  p_st_hash_report         (保存Frreport_data)
*  RTN      :  APM_L7_ANALYZE_SUCC         成功
*           :  APM_L7_ANALYZE_TODO         数据未结束
*           :  APM_L7_ANALYZE_FAIL         失败
*  NOTE     :
*******************************************************************************/
FR_STATIC int __req_data_analysis(Conntrack *p_st_conn
                                 , Apm_packet *p_st_packet
                                 , Frhash_header *p_st_hash_report
                                 , L7_session *p_st_session)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    L7session_activeMQ_private *p_st_private = NULL;
    L4_transaction *p_st_l4_trans = NULL;
    unsigned char uc_tmp = 4;
    unsigned long ul_tmp = 0;
    Frreport_data *p_st_report_data = NULL;
    char arr_info[1024];

    if(p_st_conn == NULL || p_st_packet == NULL
        || p_st_hash_report == NULL || p_st_session == NULL)
        return APM_L7_ANALYZE_FAIL;

    p_st_private = (L7session_activeMQ_private*)p_st_session->p_session_private;
    p_st_l4_trans = &(p_st_private->st_l4_trans);

    p_st_private->i_use_request = FR_YES;

    if(p_st_l4_trans->st_request_time_first_byte.tv_sec == 0
        && p_st_l4_trans->st_request_time_first_byte.tv_usec == 0)
    {
        p_st_private->p_st_body_data_req = frreport_body_new_l7(p_st_conn);
        if(p_st_private->p_st_body_data_req == NULL)
            return APM_L7_ANALYZE_FAIL;

        //st_request_time_first_byte
        p_st_l4_trans->st_request_time_first_byte.tv_sec = p_st_packet->st_timestamp.tv_sec;
        p_st_l4_trans->st_request_time_first_byte.tv_usec = p_st_packet->st_timestamp.tv_usec;

        //seq ++
        p_st_private->ul_seq = frct_seq_increase(p_st_conn);
    }
    //st_request_time
    p_st_l4_trans->st_request_time.tv_sec = p_st_packet->st_timestamp.tv_sec;
    p_st_l4_trans->st_request_time.tv_usec = p_st_packet->st_timestamp.tv_usec;

    /* 请求的字节数 */
    p_st_l4_trans->l_request_bytes += p_st_packet->ui_payload_len;

    i_ret = bytebuff_add(p_st_private->p_st_data, p_st_packet->p_payload, p_st_packet->ui_payload_len);
    if(i_ret == FR_FAIL)
        return APM_L7_ANALYZE_FAIL;
    
    //获取数据包的长度
    if(p_st_private->i_data_len == 0)
    {
        i_ret = bytebuff_get_int(p_st_private->p_st_data, 4, (uint32_t *)(&p_st_private->i_data_len), BYTE_BIG_ENDIAN);
        if(i_ret == FR_FAIL)
            return APM_L7_ANALYZE_FAIL;
        
        if(p_st_private->i_data_len == 0)
            return APM_L7_ANALYZE_FAIL;
    
        if((unsigned int)bytebuff_remaining(p_st_private->p_st_data) < (unsigned int)p_st_private->i_data_len)
            return APM_L7_ANALYZE_TODO;
    
        if((unsigned int)bytebuff_remaining(p_st_private->p_st_data) != (unsigned int)p_st_private->i_data_len)
            return APM_L7_ANALYZE_FAIL;
    }
    else
    {

        if((unsigned int)bytebuff_remaining(p_st_private->p_st_data) < (unsigned int)p_st_private->i_data_len)
            return APM_L7_ANALYZE_TODO;
    
        if((unsigned int)bytebuff_remaining(p_st_private->p_st_data) != (unsigned int)p_st_private->i_data_len)
            return APM_L7_ANALYZE_FAIL;
    }
    

    //长连接
    if(p_st_private->i_flag_try == 1)
    {
        i_ret = APM_L7_ANALYZE_FAIL;
        while(i_ret == APM_L7_ANALYZE_FAIL && uc_tmp)
        {
            //尝试并清空数据
            uc_tmp--;
            p_st_private->i_tightEncodingEnabled = uc_tmp & '\x01';
            p_st_private->i_cacheEnabled = (uc_tmp >> 1) & '\x01';
            for (p_st_private->i_openwire_version =10; p_st_private->i_openwire_version >= 9; p_st_private->i_openwire_version--)
            {
                i_ret = activeMQ_object_parse(p_st_private->p_st_data, p_st_private->p_st_body_data_resp, p_st_private, p_st_session);
                if(i_ret == APM_L7_ANALYZE_FAIL)
                {
                    frreport_body_free(&p_st_private->p_st_body_data_resp);
                    p_st_private->p_st_body_data_resp = frreport_body_new_l7(p_st_conn);
                    if(p_st_private->p_st_body_data_resp == NULL)
                        return APM_L7_ANALYZE_FAIL;
                    
                    bytebuff_rewind(p_st_private->p_st_data);
                    p_st_private->i_data_len = 0;
                }
                else if(i_ret == APM_L7_ANALYZE_SUCC)
                    break;
            }
        }
    
        if(i_ret == APM_L7_ANALYZE_FAIL)
            return APM_L7_ANALYZE_FAIL;
    }
    else
    {
        i_ret = activeMQ_object_parse(p_st_private->p_st_data, p_st_private->p_st_body_data_req, p_st_private, p_st_session);
    }

    //session 数据处理
    if(p_st_private->i_flag_data_type && i_ret == APM_L7_ANALYZE_SUCC)
    {
        if(p_st_private->i_flag_data_type == FRLOGIN_START)
        {
            p_st_private->st_login_timestamp.tv_sec = p_st_packet->st_timestamp.tv_sec;
            p_st_private->st_login_timestamp.tv_usec = p_st_packet->st_timestamp.tv_usec;

            fruuid(p_st_private->arr_session_id);
        }

        if (p_st_private->i_flag_data_type == FRLOGIN_START)
        {
            if (p_st_private->p_user_name != NULL && *p_st_private->p_user_name != '\0')
                snprintf(arr_info, sizeof(arr_info), "%s login", p_st_private->p_user_name);
            else
                snprintf(arr_info, sizeof(arr_info), "login");

            p_st_report_data = frreport_update_create(p_st_conn, APM_REPORT_UPDATE_LOGON, arr_info, &p_st_packet->st_timestamp);
        }
        else if (p_st_private->i_flag_data_type == FRLOGIN_FAIL)
        {
            if (p_st_private->p_user_name != NULL && *p_st_private->p_user_name != '\0')
                snprintf(arr_info, sizeof(arr_info), "%s login reject", p_st_private->p_user_name);
            else
                snprintf(arr_info, sizeof(arr_info), "login reject");

            p_st_report_data = frreport_update_create(p_st_conn, APM_REPORT_UPDATE_LOGON_REJECT, arr_info, &p_st_packet->st_timestamp);
        }
        else if(p_st_private->i_flag_data_type == FRLOGOUT_END)
        {

            if (p_st_private->p_user_name != NULL && *p_st_private->p_user_name != '\0')
                snprintf(arr_info, sizeof(arr_info), "%s logout", p_st_private->p_user_name);
            else
                snprintf(arr_info, sizeof(arr_info), "logout");

            p_st_report_data = frreport_update_create(p_st_conn, APM_REPORT_UPDATE_LOGOUT, arr_info, &p_st_packet->st_timestamp);
        }

        p_st_private->i_flag_data_type = 0;
        if (p_st_report_data)
        {
            if(frreport_send(g_p_apm_context, &p_st_report_data) == FR_FAIL)
            {
                frreport_data_free(&p_st_report_data);
                return APM_L7_ANALYZE_FAIL;
            }
        }
        
        return APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC);
    }

    //数据上报
    if(i_ret == APM_L7_ANALYZE_SUCC && p_st_private->i_flag_report)
    {
        i_ret = frreport_body_l7_add_connInfo(p_st_private->p_st_body_data_req, p_st_conn);
        if(i_ret == FR_FAIL)
            return APM_L7_ANALYZE_FAIL;

        if(p_st_private->i_flag_try == 1)
        {
            //判断解析方式是否可确定  否则重新把 p_st_private->i_flag_detected 置为1
            if(p_st_private->i_flag_detected == 1)
                p_st_private->i_flag_try = 0;
            else
                p_st_private->i_flag_detected = 1;
        }

        //session
        if(__activemq_session_report(p_st_private->p_st_body_data_req, p_st_private) == FR_FAIL)
            goto ERROR;


        //信息上报
        if(frreport_body_l7_add_ulong(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_ID, p_st_private->ul_seq) == FR_FAIL)
            goto ERROR;

        if(frreport_body_l7_add_timeval(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_START_TIMESTAMP, &p_st_l4_trans->st_request_time_first_byte) == FR_FAIL)
            goto ERROR;

        if(frreport_body_l7_add_timeval(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_END_TIMESTAMP, &p_st_l4_trans->st_request_time) == FR_FAIL)
            goto ERROR;

        ul_tmp = frtime_diff(&p_st_l4_trans->st_request_time_first_byte, &p_st_l4_trans->st_request_time);
        if(frreport_body_l7_add_ulong(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_SEND_TIME_US, ul_tmp) == FR_FAIL)
            goto ERROR;

        if(frreport_body_l7_add_long(p_st_private->p_st_body_data_req, APM_REPORT_KEY_REQ_DATA_BYTES, p_st_l4_trans->l_request_bytes) == FR_FAIL)
            goto ERROR;

        if(p_st_private->is_messages)
            p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_REQ);
        else
            p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_SUB);

        if(p_st_report_data == NULL)
            goto ERROR;

        p_st_private->p_lst_comm_tmp = FRREPORT_BODY_L7LIST_CLONE_COMM(p_st_private->p_st_body_data_req);
        if(p_st_private->p_lst_comm_tmp == NULL)
            goto ERROR;

        p_st_private->p_lst_req_tmp = FRREPORT_BODY_L7LIST_CLONE_REQ(p_st_private->p_st_body_data_req);
        if(p_st_private->p_lst_req_tmp == NULL)
            goto ERROR;

        if(p_st_private->is_topic)
        {
            p_st_private->p_lst_mq_tmp = FRREPORT_BODY_L7LIST_CLONE_MQ(p_st_private->p_st_body_data_req);
            if(p_st_private->p_lst_mq_tmp == NULL)
                goto ERROR;
        }

        if(frreport_data_add_body(p_st_report_data, p_st_private->p_st_body_data_req) == FR_FAIL)
            goto ERROR;
        p_st_private->p_st_body_data_req = NULL;

        if(frreport_data_hash(p_st_hash_report, p_st_report_data) == FR_FAIL)
            goto ERROR;

        bytebuff_free(&p_st_private->p_st_data);
        p_st_private->p_st_data = bytebuff_creat();
        if(p_st_private->p_st_data == NULL)
            return APM_L7_ANALYZE_FAIL;

        if(p_st_private->i_flag_responserequired == 0)
        {
            if(__activeMQ_session_reset(p_st_private) == FR_FAIL)
                goto ERROR;
        }
        else
        {
            p_st_private->i_data_len = 0;
            
            bytebuff_free(&p_st_private->p_st_data);
            p_st_private->p_st_data = bytebuff_creat();
            if(p_st_private->p_st_data == NULL)
                goto ERROR;
        }

        return APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC);
    }
    else if(i_ret == APM_L7_ANALYZE_SUCC)
    {
        if(p_st_private->i_flag_responserequired == 0)
        {
            if(__activeMQ_session_reset(p_st_private) == FR_FAIL)
                goto ERROR;
        }
        else
        {
            p_st_private->i_data_len = 0;
            
            bytebuff_free(&p_st_private->p_st_data);
            p_st_private->p_st_data = bytebuff_creat();
            if(p_st_private->p_st_data == NULL)
                goto ERROR;
        }
    
        return APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC);
    }

    return i_ret;

ERROR:
    if(p_st_report_data)
        frreport_data_free(&p_st_report_data);

    if(p_st_private->p_lst_comm_tmp)
        frreport_body_l7list_free(&p_st_private->p_lst_comm_tmp);

    if(p_st_private->p_lst_req_tmp)
        frreport_body_l7list_free(&p_st_private->p_lst_req_tmp);

    if(p_st_private->p_lst_mq_tmp)
        frreport_body_l7list_free(&p_st_private->p_lst_mq_tmp);

    return APM_L7_ANALYZE_FAIL;

}


/*******************************************************************************
*  FUNC     :  activeMQ resp 的packet解析
*  ARGS     :  p_st_conn                (入力链接信息)
            :  p_st_packet             (入力packet信息)
            :  p_st_hash_report         (保存Frreport_data)
*  RTN      :  APM_L7_ANALYZE_SUCC         成功
*           :  APM_L7_ANALYZE_TODO         数据未结束
*           :  APM_L7_ANALYZE_FAIL         失败
*  NOTE     :
*******************************************************************************/
FR_STATIC int __resp_data_analysis(Conntrack *p_st_conn
                                 , Apm_packet *p_st_packet
                                 , Frhash_header *p_st_hash_report
                                 , L7_session *p_st_session)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    L7session_activeMQ_private *p_st_private = NULL;
    L4_transaction *p_st_l4_trans = NULL;
    unsigned char uc_tmp = 4;
    unsigned long ul_tmp = 0;
    Frreport_data *p_st_report_data = NULL;

    if(p_st_conn == NULL || p_st_packet == NULL
        || p_st_hash_report == NULL || p_st_session == NULL)
        return APM_L7_ANALYZE_FAIL;

    p_st_private = (L7session_activeMQ_private*)p_st_session->p_session_private;
    p_st_l4_trans = &(p_st_private->st_l4_trans);

    p_st_private->i_use_request = FR_NO;

    if (p_st_l4_trans->st_response_time_first_byte.tv_sec == 0 &&
        p_st_l4_trans->st_response_time_first_byte.tv_usec == 0)
    {
        p_st_private->p_st_body_data_resp = frreport_body_new_l7(p_st_conn);
        if(p_st_private->p_st_body_data_resp == NULL)
            return APM_L7_ANALYZE_FAIL;

        //应答发起时间戳
        p_st_l4_trans->st_response_time_first_byte.tv_sec = p_st_packet->st_timestamp.tv_sec;
        p_st_l4_trans->st_response_time_first_byte.tv_usec = p_st_packet->st_timestamp.tv_usec;

        //首包时间
        if(p_st_l4_trans->st_request_time_first_byte.tv_sec != 0)
            p_st_l4_trans->ul_response_time_firstbyte_us = frtime_diff(&(p_st_l4_trans->st_request_time_first_byte), &(p_st_l4_trans->st_response_time_first_byte));
        else
            p_st_l4_trans->ul_response_time_firstbyte_us = 0;

    }
    //st_response_time
    p_st_l4_trans->st_response_time.tv_sec = p_st_packet->st_timestamp.tv_sec;
    p_st_l4_trans->st_response_time.tv_usec = p_st_packet->st_timestamp.tv_usec;
    /* 请求的字节数 */
    p_st_l4_trans->l_response_bytes += p_st_packet->ui_payload_len;

    //添加到bytebyff
    i_ret = bytebuff_add(p_st_private->p_st_data, p_st_packet->p_payload, p_st_packet->ui_payload_len);
    if(i_ret == FR_FAIL)
        return APM_L7_ANALYZE_FAIL;
    
    //获取数据包的长度
    if(p_st_private->i_data_len == 0)
    {
        i_ret = bytebuff_get_int(p_st_private->p_st_data, 4, (uint32_t *)(&p_st_private->i_data_len), BYTE_BIG_ENDIAN);
        if(i_ret == FR_FAIL)
            return APM_L7_ANALYZE_FAIL;
        
        if(p_st_private->i_data_len == 0)
            return APM_L7_ANALYZE_FAIL;
        
        if((unsigned int)bytebuff_remaining(p_st_private->p_st_data) < (unsigned int)p_st_private->i_data_len)
            return APM_L7_ANALYZE_TODO;
        
        if((unsigned int)bytebuff_remaining(p_st_private->p_st_data) != (unsigned int)p_st_private->i_data_len)
            return APM_L7_ANALYZE_FAIL;
        
    }
    else
    {
        
        if((unsigned int)bytebuff_remaining(p_st_private->p_st_data) < (unsigned int)p_st_private->i_data_len)
            return APM_L7_ANALYZE_TODO;
        
        if((unsigned int)bytebuff_remaining(p_st_private->p_st_data) != (unsigned int)p_st_private->i_data_len)
            return APM_L7_ANALYZE_FAIL;
        
    }
    
    //object 解析
    if(p_st_private->i_flag_try == 1)
    {
        i_ret = APM_L7_ANALYZE_FAIL;
        while(i_ret == APM_L7_ANALYZE_FAIL && uc_tmp)
        {
            //尝试并清空数据
            uc_tmp--;
            p_st_private->i_tightEncodingEnabled = uc_tmp & '\x01';
            p_st_private->i_cacheEnabled = (uc_tmp >> 1) & '\x01';
            for (p_st_private->i_openwire_version = 10; p_st_private->i_openwire_version >= 9; p_st_private->i_openwire_version--)
            {
                i_ret = activeMQ_object_parse(p_st_private->p_st_data, p_st_private->p_st_body_data_resp, p_st_private, p_st_session);
                if(i_ret == APM_L7_ANALYZE_FAIL)
                {
                    frreport_body_free(&p_st_private->p_st_body_data_resp);
                    p_st_private->p_st_body_data_resp = frreport_body_new_l7(p_st_conn);
                    if(p_st_private->p_st_body_data_resp == NULL)
                        return APM_L7_ANALYZE_FAIL;

                    bytebuff_rewind(p_st_private->p_st_data);
                    p_st_private->i_data_len = 0;
                }
                else if(i_ret == APM_L7_ANALYZE_SUCC)
                    break;
            }
        }
    
        if(i_ret == APM_L7_ANALYZE_FAIL)
            return APM_L7_ANALYZE_FAIL;
    }
    else
    {
        i_ret = activeMQ_object_parse(p_st_private->p_st_data, p_st_private->p_st_body_data_resp, p_st_private, p_st_session);
    }

    //数据上报
    if(i_ret == APM_L7_ANALYZE_SUCC && p_st_private->i_flag_report)
    {
        if(p_st_private->i_flag_try == 1)
        {
            //判断解析方式是否可确定  否则重新把 p_st_private->i_flag_detected 置为1
            if(p_st_private->i_flag_detected == 1)
                p_st_private->i_flag_try = 0;
            else
                p_st_private->i_flag_detected = 1;
        }

        if(p_st_private->ul_seq == 0 || p_st_private->p_lst_comm_tmp == NULL)
            p_st_private->ul_seq = frct_seq_increase(p_st_conn);

        //数据链表  设置为response
        if(p_st_private->p_lst_comm_tmp == NULL)
        {
            i_ret = frreport_body_l7_add_connInfo(p_st_private->p_st_body_data_resp, p_st_conn);
            if(i_ret == FR_FAIL)
                goto ERROR;

            if(frreport_body_l7_add_ulong(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_SERVER_TIME_US, 0) == FR_FAIL)
                goto ERROR;

            if(__activemq_session_report(p_st_private->p_st_body_data_resp, p_st_private) == FR_FAIL)
                goto ERROR;
        }
        else
        {
            //comm
            if(FRREPORT_BODY_L7LIST_SET_COMM(p_st_private->p_st_body_data_resp, &p_st_private->p_lst_comm_tmp) == FR_FAIL)
                goto ERROR;

            //把req数据链表添加到resp中
            if(FRREPORT_BODY_L7LIST_SET_REQ(p_st_private->p_st_body_data_resp, &p_st_private->p_lst_req_tmp) == FR_FAIL)
                goto ERROR;

            if(p_st_private->is_topic)
            {
                if(FRREPORT_BODY_L7LIST_SET_MQ(p_st_private->p_st_body_data_resp, &p_st_private->p_lst_mq_tmp) == FR_FAIL)
                    goto ERROR;
            }

            ul_tmp = frtime_diff(&p_st_l4_trans->st_request_time, &p_st_l4_trans->st_response_time_first_byte);
            if(frreport_body_l7_add_ulong(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_SERVER_TIME_US, ul_tmp) == FR_FAIL)
                goto ERROR;

        }

        if(frreport_body_l7_add_timeval(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_START_TIMESTAMP, &p_st_l4_trans->st_response_time_first_byte) == FR_FAIL)
            goto ERROR;

        if(frreport_body_l7_add_timeval(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_END_TIMESTAMP, &p_st_l4_trans->st_response_time) == FR_FAIL)
            goto ERROR;

        ul_tmp = frtime_diff(&p_st_l4_trans->st_response_time_first_byte, &p_st_l4_trans->st_response_time);
        if(frreport_body_l7_add_ulong(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_DOWNLOAD_TIME_US, ul_tmp) == FR_FAIL)
            goto ERROR;

        if(frreport_body_l7_add_long(p_st_private->p_st_body_data_resp, APM_REPORT_KEY_RESP_DATA_BYTES, p_st_l4_trans->l_response_bytes) == FR_FAIL)
            goto ERROR;

        if(p_st_private->is_messages)
            p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_NOTIFY);
        else
            p_st_report_data = frreport_data_l7_new(FRREPORT_TYPE_L7DATA_RESP);

        if(p_st_report_data == NULL)
            goto ERROR;

        if(frreport_data_add_body(p_st_report_data, p_st_private->p_st_body_data_resp) == FR_FAIL)
            goto ERROR;
        p_st_private->p_st_body_data_resp = NULL;

        if(frreport_data_hash(p_st_hash_report, p_st_report_data) == FR_FAIL)
            goto ERROR;

//        i_ret = __activeMQ_data_format(p_st_conn, FR_NO, p_st_hash_report, p_st_private, p_st_private->ul_seq, &p_st_packet->st_timestamp);
//        if(i_ret == FR_FAIL)
//            return APM_L7_ANALYZE_FAIL;

        if(__activeMQ_session_reset(p_st_private) == FR_FAIL)
            goto ERROR;

        return APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC);
    }
    else if(i_ret == APM_L7_ANALYZE_SUCC)
    {
        if(__activeMQ_session_reset(p_st_private) == FR_FAIL)
            goto ERROR;
    
        return APM_L7_ANALYZE_SET_RESP(APM_L7_ANALYZE_SUCC);
    }

    return i_ret;

ERROR:
    if(p_st_report_data)
        frreport_data_free(&p_st_report_data);

    return APM_L7_ANALYZE_FAIL;
}
/*******************************************************************************
*  FUNC     :  activeMQ的packet解析
*  ARGS     :  p_st_conn                (入力链接信息)
            :  p_st_packet             (入力packet信息)
            :  p_st_hash_report         (保存Frreport_data)
*  RTN      :  APM_L7_ANALYZE_SUCC         成功
*           :  APM_L7_ANALYZE_TODO         数据未结束
*           :  APM_L7_ANALYZE_FAIL         失败
*  NOTE     :
*******************************************************************************/
int activeMQ_analysis(Conntrack *p_st_conn
                  , Apm_packet *p_st_packet
                  , Frhash_header *p_st_hash_report)
{
    int i_ret = APM_L7_ANALYZE_SUCC;
    L7_session *p_st_session = NULL;

    if(p_st_conn == NULL || p_st_packet == NULL
        || p_st_hash_report == NULL)
        return APM_L7_ANALYZE_FAIL;

    /* 到Conntrack中取得session信息 */
    p_st_session = frct_get_session(p_st_conn);
    if (p_st_session == NULL)
        return APM_L7_ANALYZE_FAIL;

    //fin syn 包  销毁私有数据
    if(p_st_packet->em_analyz_cmd == FRANALYZ_CMD_FIN || p_st_packet->em_analyz_cmd == FRANALYZ_CMD_SYN)
    {
        activeMQ_session_destroy((void *)(p_st_session->p_session_private));
        p_st_session->p_session_private = NULL;
        return APM_L7_ANALYZE_SUCC;
    }
    
    //p_session_private   分配内存
    if(p_st_session->p_session_private == NULL)
    {
        p_st_session->p_session_private = __activeMQ_private_create();
        //内存分配失败
        if(p_st_session->p_session_private == NULL)
            return APM_L7_ANALYZE_FAIL;
    }
    
    //request
    if(p_st_packet->i_direction == FR_DIRECT_CLIENT2SRV)
    {
        i_ret = __req_data_analysis(p_st_conn,p_st_packet,p_st_hash_report,p_st_session);
        if(i_ret == APM_L7_ANALYZE_FAIL)
            goto ERROR;

        return i_ret;
    }
    else//response
    {
        i_ret = __resp_data_analysis(p_st_conn,p_st_packet,p_st_hash_report, p_st_session);
        if (i_ret == APM_L7_ANALYZE_FAIL)
            goto ERROR;

        return i_ret;
    }

ERROR:

    if(__activeMQ_session_reset(p_st_session->p_session_private) == FR_FAIL)
    {
        if(p_st_session->p_session_private)
            activeMQ_session_destroy(p_st_session->p_session_private);
        p_st_session->p_session_private = NULL;
    }

    return APM_L7_ANALYZE_FAIL;

}

/*******************************************************************************
*  FUNC     :  根据response数据判断是否为activemq数据
*  ARGS     :  p_indata                     输入数据信息
*           :  ui_data_len                  输入数据铲毒
*  RTN      :  APM_L7_IS_NO                 不是activeMQ数据
*           :  APM_L7_IS_YES                是activeMQ数据
*  NOTE     :
*******************************************************************************/
FR_STATIC int __response_data_detect(char *p_indata, unsigned int ui_data_len)
{
    unsigned char uc_tmp = 0;
    unsigned short us_boolean_data_len = 0;
    unsigned int ui_len = 0;

    if(p_indata == NULL || ui_data_len == 0)
        return APM_L7_IS_NO;

    uc_tmp = *p_indata;
    //tight_marshal
    if((unsigned char)(uc_tmp & 0xff) != 0)
    {
        us_boolean_data_len = 0xff & uc_tmp;
        //boolean + 1 + command id + CorrelationId
        ui_len = us_boolean_data_len + 1 + 4 + 4;
        if(ui_len == ui_data_len)
            return APM_L7_IS_YES;
    }

    //loose_marshal
    ui_len = 1 + 4 + 4;
    if(ui_len == ui_data_len)
        return APM_L7_IS_YES;

    return APM_L7_IS_NO;
}

/*******************************************************************************
*  FUNC     :  判断是否是activeMQ
*  ARGS     :  p_st_conn                    输入数据信息
*           :  p_st_packet                 输入数据
*  RTN      :  APM_L7_IS_NO                 不是activeMQ数据
*           :  APM_L7_IS_YES                是activeMQ数据
*  NOTE     :
*******************************************************************************/
int activeMQ_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet)
{
    unsigned int ui_data_len = 0;
    int i_ret = 0;

    //入力参数检查
    if(p_st_packet == NULL || p_st_conn==NULL)
        return APM_L7_IS_NO;

    if(p_st_packet->ui_payload_len < 4)
        return APM_L7_IS_NO;

    ui_data_len = pntohi(p_st_packet->p_payload);
    if(ui_data_len != (p_st_packet->ui_payload_len - 4))
        return APM_L7_IS_NO;

    if(ui_data_len <= 0)
        return APM_L7_IS_NO;

    if(*(p_st_packet->p_payload + 4) == '\x01')
    {
        if(p_st_packet->ui_payload_len >= 13 && memcmp(p_st_packet->p_payload + 5, "ActiveMQ", 8) == 0)
            return APM_L7_IS_YES;
        else
            return APM_L7_IS_NO;
    }

    //长连接detect
    else if(*(p_st_packet->p_payload + 4) == '\x1e')
    {

        i_ret = __response_data_detect(p_st_packet->p_payload + 5, p_st_packet->ui_payload_len - 5);
        if(i_ret == APM_L7_IS_NO)
            return APM_L7_IS_NO;

        i_ret = frct_get_direct_known(p_st_conn);
        if(i_ret == FR_DIRECT_UNKNOWN)
            frcap_set_cs_srv(p_st_conn, p_st_packet->src_ip, p_st_packet->src_port);

        frservice_add(g_p_apm_context, MODULE_L7PROTO, p_st_packet->src_ip, p_st_packet->src_port, NULL, 0);

        return APM_L7_IS_YES;
    }
    else if(*(p_st_packet->p_payload + 4) == '\x15')
    {
        i_ret = openwire_messagedispatch_detect(p_st_packet->p_payload + 5, p_st_packet->ui_payload_len - 5, p_st_conn);
        if(i_ret == FR_FAIL)
            return APM_L7_IS_NO;

        i_ret = frct_get_direct_known(p_st_conn);
        if(i_ret == FR_DIRECT_UNKNOWN)
            frcap_set_cs_srv(p_st_conn, p_st_packet->src_ip, p_st_packet->src_port);

        frservice_add(g_p_apm_context, MODULE_L7PROTO, p_st_packet->src_ip, p_st_packet->src_port, NULL, 0);

        return APM_L7_IS_YES;
    }

    if (ui_data_len == 0)
        return APM_L7_IS_NO;

    i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->dst_ip, p_st_packet->dst_port, NULL, 0, NULL);
    if (i_ret == FR_SUCC)
    {
        if (frct_get_direct_known(p_st_conn) == FR_DIRECT_UNKNOWN)
            frcap_set_cs_srv(p_st_conn, p_st_packet->dst_ip, p_st_packet->dst_port);

        return APM_L7_IS_YES;
    }

    i_ret = frservice_get(g_p_apm_context, MODULE_L7PROTO, p_st_packet->src_ip, p_st_packet->src_port, NULL, 0, NULL);
    if (i_ret == FR_SUCC)
    {
        if (frct_get_direct_known(p_st_conn) == FR_DIRECT_UNKNOWN)
        frcap_set_cs_srv(p_st_conn, p_st_packet->src_ip, p_st_packet->src_port);
        return APM_L7_IS_YES;
    }

    return APM_L7_IS_NO;

}

/*******************************************************************************
*  FUNC     :  私有信息销毁
*  ARGS     :  p_private_session(activeMQ的私有session数据)
*  RTN      :  FR_SUCC
*  NOTE     :
*******************************************************************************/
int activeMQ_session_destroy(void *p_private_session)
{
    L7session_activeMQ_private *p_st_private = (L7session_activeMQ_private*)p_private_session;

    if(p_st_private)
    {
        if(p_st_private->p_st_body_data_req)
            frreport_body_free(&p_st_private->p_st_body_data_req);

        if(p_st_private->p_st_body_data_resp)
            frreport_body_free(&p_st_private->p_st_body_data_resp);

        if(p_st_private->p_lst_comm_tmp)
            frreport_body_l7list_free(&p_st_private->p_lst_comm_tmp);

        if(p_st_private->p_lst_req_tmp)
            frreport_body_l7list_free(&p_st_private->p_lst_req_tmp);

        if(p_st_private->p_lst_mq_tmp)
            frreport_body_l7list_free(&p_st_private->p_lst_mq_tmp);

        if(p_st_private->p_st_data)
            bytebuff_free(&p_st_private->p_st_data);

        if(p_st_private->p_user_name)
            frfree(p_st_private->p_user_name);

        if(p_st_private->p_server)
            frfree(p_st_private->p_server);

        if(p_st_private->p_title)
            frfree(p_st_private->p_title);

        frfree(p_st_private);
    }
    return FR_SUCC;
}

